﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using GPSTrackingWebsiteManager.ClassManager;
using Telerik.Web.UI;

namespace GPSTrackingWebsiteManager.Manager.UserControls
{
    public partial class GPSSearchCombo : System.Web.UI.UserControl
    {
        //Events
        public delegate void AcceptEvent(List<GridDataItem> rows);
        public event AcceptEvent OnAcceptEvent;

        public delegate void CancelEvent();
        public event CancelEvent OnCancelEvent;

        //Propeties
        [Browsable(true), Category("Custom")]
        public bool IsMultipleSelects { get; set; }

        [Browsable(true), Category("Custom")]
        public string CalledBy { get; set; }

        [Browsable(true), Category("Custom")]
        public bool IsClearGridAfterClose { get; set; }

        //Events
        protected void Page_Load(object sender, EventArgs e)
        {
            GPSGrid.AllowMultiRowSelection = IsMultipleSelects;

            if (!IsPostBack)
            {
                // Get all types
                GetAllTypes();

                // Get all Models
                GetAllModels();

                GridSortExpression expression = new GridSortExpression();
                expression.FieldName = GPSTrackingDBV2.Entities.GpsStockColumn.Name.ToString();
                expression.SetSortOrder("Ascending");
                this.GPSGrid.MasterTableView.SortExpressions.AddSortExpression(expression);

                if (IsClearGridAfterClose)
                    clearGPSGrid();
            }
        }

        protected void btnGPSSearch_Click(object sender, EventArgs e)
        {
            getInfoGrid();
            GPSGrid.DataBind();
        }

        protected void btnGPSOK_Click(object sender, EventArgs e)
        {
            List<GridDataItem> rows = new List<GridDataItem>();
            foreach (Telerik.Web.UI.GridDataItem selectedItem in GPSGrid.SelectedItems)
            {
                rows.Add(selectedItem);
            }

            if (IsClearGridAfterClose)
                clearGPSGrid();

            OnAcceptEvent(rows);
        }

        protected void btnGPSCancel_Click(object sender, EventArgs e)
        {
            if (IsClearGridAfterClose)
                clearGPSGrid();
            OnCancelEvent();
        }

        //Methods

        public void clearGPSGrid()
        {
            GPSGrid.DataSource = new string[] { };
            GPSGrid.DataBind();
        }

        public void getGPSToSelect()
        {
            try
            {
                GPSGrid.DataSource = GPSTrackingDBV2Custom.Data.DataRepository.GPSStockProvider.GetByCompanyIDToAssociateVPCControlFind(getSessionManager().CompanyIDSelectedComboBox, txtSearchImei.Text, txtSearchName.Text, txtSearchSimNumber.Text, txtSearchDescription.Text);
            }
            catch (Exception ex)
            {
                ClassManager.CustomExceptionManager.Generate_Error_Object_RadGrid(ex.Message, ref GPSGrid);
            }
        }

        protected SessionManager getSessionManager()
        {
            return (SessionManager)HttpContext.Current.Session["SessionManager"];
        }

        protected void getInfoGrid()
        {
            if (CalledBy == "GPSGroupsByGPSStock")
            {
                GetGPSGridToAssociate();
            }
            if (CalledBy == "UsersByGPSStocks")
            {
                GetGPSGridByGPSGroupsToAssociate();
            }
        }

        private void GetAllTypes()
        {
            GPSTrackingDBV2.Entities.TList<GPSTrackingDBV2.Entities.GpsType> GpsTypeEntities = GPSTrackingDBV2.Data.DataRepository.GpsTypeProvider.GetAll();
            GPSTrackingDBV2.Entities.GpsType GpsTypeEntity = new GPSTrackingDBV2.Entities.GpsType();
            GpsTypeEntity.GpsTypeId = -1;
            GpsTypeEntity.Name = "- Lista Tipos -";

            GpsTypeEntities.Add(GpsTypeEntity);
            GpsTypeEntities.Sort(GPSTrackingDBV2.Entities.GpsTypeColumn.Name.ToString() + " Asc");

            cmbGPSType.DataSource = GpsTypeEntities;
            cmbGPSType.DataBind();
        }

        private void GetAllModels()
        {
            GPSTrackingDBV2.Entities.TList<GPSTrackingDBV2.Entities.GpsModels> GpsModelsEntities = GPSTrackingDBV2.Data.DataRepository.GpsModelsProvider.GetAll();
            GPSTrackingDBV2.Entities.GpsModels GpsModelsEntity = new GPSTrackingDBV2.Entities.GpsModels();
            GpsModelsEntity.GpsModelId = -1;
            GpsModelsEntity.Name = "- Lista Modelos -";

            GpsModelsEntities.Add(GpsModelsEntity);
            GpsModelsEntities.Sort(GPSTrackingDBV2.Entities.GpsModelsColumn.Name.ToString() + " Asc");

            cmbGPSModel.DataSource = GpsModelsEntities;
            cmbGPSModel.DataBind();
        }

        protected void GetGPSGridToAssociate() {
            int gpsTypeID = -1;
            int gpsModelID = -1;
            if (cmbGPSType.SelectedValue != "-1") gpsTypeID = int.Parse(cmbGPSType.SelectedValue);
            if (cmbGPSModel.SelectedValue != "-1") gpsModelID = int.Parse(cmbGPSModel.SelectedValue);

            GPSGrid.DataSource = GPSTrackingDBV2Custom.Data.DataRepository.GPSStockProvider.GetGPSStockToAssociateGPSGroupsFind(txtSearchImei.Text, txtSearchName.Text, txtSearchSimNumber.Text, txtSearchDescription.Text, gpsTypeID, gpsModelID);
        }

        protected void GetGPSGridByGPSGroupsToAssociate()
        {
            int gpsTypeID = -1;
            int gpsModelID = -1;

            if (cmbGPSType.SelectedValue != "-1") gpsTypeID = int.Parse(cmbGPSType.SelectedValue);
            if (cmbGPSModel.SelectedValue != "-1") gpsModelID = int.Parse(cmbGPSModel.SelectedValue);

            GPSGrid.DataSource = GPSTrackingDBV2Custom.Data.DataRepository.GPSStockProvider.GetGPSStockToAssociateUsersByGPSGroupID_Find(getSessionManager().CompanyIDSelectedComboBox, getSessionManager().UsersByGPSStock_GPSStockComboSearch_username, getSessionManager().UsersByGPSStock_GPSStockComboSearch_GPSGroupID, txtSearchImei.Text, txtSearchName.Text, txtSearchSimNumber.Text, txtSearchDescription.Text, gpsTypeID, gpsModelID);
        }

        protected void GPSGrid_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            getInfoGrid();
        }
    }
}